package com.ruoyi.guarantee.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.guarantee.domain.BAgreement;
import com.ruoyi.guarantee.domain.BAgreementLog;
import com.ruoyi.guarantee.mapper.BAgreementLogMapper;
import com.ruoyi.guarantee.mapper.BAgreementMapper;
import com.ruoyi.guarantee.service.IDiffAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/********************************************************************************
 ** author： zhangtianhe
 ** date：2024年12月20日 16:50
 ** description&desc：
 *********************************************************************************/
@Service
public class DiffAgreementServiceImpl implements IDiffAgreementService {
    @Autowired
    private BAgreementMapper bAgreementMapper;
    @Autowired
    private BAgreementLogMapper bAgreementLogMapper;
    @Override
    public int diffAgreement(BAgreement oldAgreement, BAgreement newAgreement,String userName) {
        Long id = newAgreement.getId();


        if (StringUtils.isNotEmpty(oldAgreement.getXmChType())||StringUtils.isNotEmpty(newAgreement.getXmChType())){//原来不为空
            if (StringUtils.isNotEmpty(oldAgreement.getXmChType())&&StringUtils.isNotEmpty(newAgreement.getXmChType())){//新的值不为空
                if (!oldAgreement.getXmChType().equals(newAgreement.getXmChType())){
                    insertBAgreementLog(id,"xm_ch_type","出函类型",
                            newAgreement.getXmChType(),oldAgreement.getXmChType(),userName);
                }
            }else {
                insertBAgreementLog(id,"xm_ch_type","出函类型",
                        newAgreement.getXmChType(),oldAgreement.getXmChType(),userName);
            }
        }
        if (StringUtils.isNotEmpty(oldAgreement.getIsBulu())||StringUtils.isNotEmpty(newAgreement.getIsBulu())){//原来不为空
            if (StringUtils.isNotEmpty(oldAgreement.getIsBulu())&&StringUtils.isNotEmpty(newAgreement.getIsBulu())){//新的值不为空
                if (!oldAgreement.getIsBulu().equals(newAgreement.getIsBulu())){
                    insertBAgreementLog(id,"is_bulu","是否补录",
                            newAgreement.getXmChType(),oldAgreement.getXmChType(),userName);
                }
            }else {
                insertBAgreementLog(id,"is_bulu","是否补录",
                        newAgreement.getIsBulu(),oldAgreement.getIsBulu(),userName);
            }
        }

        if (oldAgreement.getXmChDjuser()!=null||newAgreement.getXmChDjuser()!=null){//原来不为空
            if (oldAgreement.getXmChDjuser()!=null&&newAgreement.getXmChDjuser()!=null){//新的值不为空
                if (oldAgreement.getXmChDjuser()!=newAgreement.getXmChDjuser()){
                    insertBAgreementLog(id,"xm_ch_djuser","出函机构及对接人",
                            newAgreement.getXmChDjuser().toString(),oldAgreement.getXmChDjuser().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"xm_ch_djuser","出函机构及对接人",
                        safeToString(newAgreement.getXmChDjuser()),safeToString(oldAgreement.getXmChDjuser()),userName);
            }
        }

        if (oldAgreement.getXmBhType()!=null||newAgreement.getXmBhType()!=null){//原来不为空
            if (oldAgreement.getXmBhType()!=null&&newAgreement.getXmBhType()!=null){//新的值不为空
                if (!oldAgreement.getXmBhType().equals(newAgreement.getXmBhType())){
                    insertBAgreementLog(id,"xm_bh_type","保函类型",
                            newAgreement.getXmBhType(),oldAgreement.getXmBhType(),userName);
                }
            }else {
                insertBAgreementLog(id,"xm_bh_type","保函类型",
                        newAgreement.getXmBhType(),oldAgreement.getXmBhType(),userName);
            }
        }
        if (oldAgreement.getXmSyName()!=null||newAgreement.getXmSyName()!=null){//原来不为空
            if (oldAgreement.getXmSyName()!=null&&newAgreement.getXmSyName()!=null){//新的值不为空
                if (!oldAgreement.getXmSyName().equals(newAgreement.getXmSyName())){
                    insertBAgreementLog(id,"xm_sy_name","受益人名称",
                            newAgreement.getXmSyName(),oldAgreement.getXmSyName(),userName);
                }
            }else {
                insertBAgreementLog(id,"xm_sy_name","受益人名称",
                        newAgreement.getXmSyName(),oldAgreement.getXmSyName(),userName);
            }
        }
        if (oldAgreement.getXmSyType()!=null||newAgreement.getXmSyType()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmSyType()).equals(safeToString(newAgreement.getXmSyType()))){
                insertBAgreementLog(id,"xm_sy_type","受益人类型",
                        safeToString(newAgreement.getXmSyType()),safeToString(oldAgreement.getXmSyType()),userName);
            }
        }
        if (oldAgreement.getXmName()!=null||newAgreement.getXmName()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmName()).equals(safeToString(newAgreement.getXmName()))){
                insertBAgreementLog(id,"xm_name","项目名称",
                        safeToString(newAgreement.getXmName()),safeToString(oldAgreement.getXmName()),userName);
            }
        }
        if (oldAgreement.getXmNameLot()!=null||newAgreement.getXmNameLot()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmNameLot()).equals(safeToString(newAgreement.getXmNameLot()))){
                insertBAgreementLog(id,"xm_name_lot","标段",
                        safeToString(newAgreement.getXmNameLot()),safeToString(oldAgreement.getXmNameLot()),userName);
            }
        }

        if (oldAgreement.getXmBhqx()!=null||newAgreement.getXmBhqx()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmBhqx()).equals(safeToString(newAgreement.getXmBhqx()))){
                insertBAgreementLog(id,"xm_bhqx","保函期限",
                        safeToString(newAgreement.getXmBhqx()),safeToString(oldAgreement.getXmBhqx()),userName);
            }
        }

        if (oldAgreement.getXmBhyjdz()!=null||newAgreement.getXmBhyjdz()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmBhyjdz()).equals(safeToString(newAgreement.getXmBhyjdz()))){
                insertBAgreementLog(id,"xm_bhyjdz","纸质保函邮寄地址",
                        safeToString(newAgreement.getXmBhyjdz()),safeToString(oldAgreement.getXmBhyjdz()),userName);
            }
        }

        if (oldAgreement.getXmSheng()!=null||newAgreement.getXmSheng()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmSheng()).equals(safeToString(newAgreement.getXmSheng()))){
                insertBAgreementLog(id,"xm_sheng","项目所在地省",
                        safeToString(newAgreement.getXmSheng()),safeToString(oldAgreement.getXmSheng()),userName);
            }
        }
        if (oldAgreement.getXmShi()!=null||newAgreement.getXmShi()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmShi()).equals(safeToString(newAgreement.getXmShi()))){
                insertBAgreementLog(id,"xm_shi","项目所在地市",
                        safeToString(newAgreement.getXmShi()),safeToString(oldAgreement.getXmShi()),userName);
            }
        }
        if (oldAgreement.getXmAddress()!=null||newAgreement.getXmAddress()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmAddress()).equals(safeToString(newAgreement.getXmAddress()))){
                insertBAgreementLog(id,"xm_address","项目所在地",
                        safeToString(newAgreement.getXmAddress()),safeToString(oldAgreement.getXmAddress()),userName);
            }
        }
        if (oldAgreement.getXmType()!=null||newAgreement.getXmType()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmType()).equals(safeToString(newAgreement.getXmType()))){
                insertBAgreementLog(id,"xm_type","项目类型",
                        safeToString(newAgreement.getXmType()),safeToString(oldAgreement.getXmType()),userName);
            }
        }
        if (oldAgreement.getXmSpecial()!=null||newAgreement.getXmSpecial()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmSpecial()).equals(safeToString(newAgreement.getXmSpecial()))){
                insertBAgreementLog(id,"xm_special","是否特殊项目",
                        safeToString(newAgreement.getXmSpecial()),safeToString(oldAgreement.getXmSpecial()),userName);
            }
        }
        if (oldAgreement.getXmSpecialName()!=null||newAgreement.getXmSpecialName()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmSpecialName()).equals(safeToString(newAgreement.getXmSpecialName()))){
                insertBAgreementLog(id,"xm_special_name","特殊项目名称",
                        safeToString(newAgreement.getXmSpecialName()),safeToString(oldAgreement.getXmSpecialName()),userName);
            }
        }
        if (oldAgreement.getXmDate()!=null||newAgreement.getXmDate()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmDate()).equals(safeToString(newAgreement.getXmDate()))){
                insertBAgreementLog(id,"xm_date","中标时间或合同签署时间",
                        dateToString(newAgreement.getXmDate()),dateToString(oldAgreement.getXmDate()),userName);
            }
        }
        if (oldAgreement.getXmMoney()!=null||newAgreement.getXmMoney()!=null){//原来不为空
            if (oldAgreement.getXmMoney()!=null&&newAgreement.getXmMoney()!=null){//新的值不为空
                if (oldAgreement.getXmMoney().doubleValue()!=newAgreement.getXmMoney().doubleValue()){
                    insertBAgreementLog(id,"xm_money","中标金额",
                            newAgreement.getXmMoney().toString(),oldAgreement.getXmMoney().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"xm_money","中标金额",
                        safeToString(newAgreement.getXmMoney()),safeToString(oldAgreement.getXmMoney()),userName);
            }
        }
        if (oldAgreement.getXmBhProportion()!=null||newAgreement.getXmBhProportion()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmBhProportion()).equals(safeToString(newAgreement.getXmBhProportion()))){
                insertBAgreementLog(id,"xm_bh_proportion","保函比例",
                        safeToString(newAgreement.getXmBhProportion()),safeToString(oldAgreement.getXmBhProportion()),userName);
            }
        }
        if (oldAgreement.getXmGongqi()!=null||newAgreement.getXmGongqi()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmGongqi()).equals(safeToString(newAgreement.getXmGongqi()))){
                insertBAgreementLog(id,"xm_gongqi","工期",
                        safeToString(newAgreement.getXmGongqi()),safeToString(oldAgreement.getXmGongqi()),userName);
            }
        }
        if (oldAgreement.getXmArticleUrl()!=null||newAgreement.getXmArticleUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmArticleUrl()).equals(safeToString(newAgreement.getXmArticleUrl()))){
                insertBAgreementLog(id,"xm_article_url","招文上传",
                        safeToString(newAgreement.getXmArticleUrl()),safeToString(oldAgreement.getXmArticleUrl()),userName);
            }
        }
        if (oldAgreement.getXmZbUrl()!=null||newAgreement.getXmZbUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmZbUrl()).equals(safeToString(newAgreement.getXmZbUrl()))){
                insertBAgreementLog(id,"xm_zb_url","上传中标通知url",
                        safeToString(newAgreement.getXmZbUrl()),safeToString(oldAgreement.getXmZbUrl()),userName);
            }
        }
        if (oldAgreement.getXmContractUrl()!=null||newAgreement.getXmContractUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmContractUrl()).equals(safeToString(newAgreement.getXmContractUrl()))){
                insertBAgreementLog(id,"xm_contract_ul","合同文件url",
                        safeToString(newAgreement.getXmContractUrl()),safeToString(oldAgreement.getXmContractUrl()),userName);
            }
        }
        if (oldAgreement.getXmSyUrl()!=null||newAgreement.getXmSyUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmSyUrl()).equals(safeToString(newAgreement.getXmSyUrl()))){
                insertBAgreementLog(id,"xm_sy_url","受益人名称依据url",
                        safeToString(newAgreement.getXmSyUrl()),safeToString(oldAgreement.getXmSyUrl()),userName);
            }
        }
        if (oldAgreement.getXmXmmcyjUrl()!=null||newAgreement.getXmXmmcyjUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmXmmcyjUrl()).equals(safeToString(newAgreement.getXmXmmcyjUrl()))){
                insertBAgreementLog(id,"xm_xmmcyj_url","项目名称依据url",
                        safeToString(newAgreement.getXmXmmcyjUrl()),safeToString(oldAgreement.getXmXmmcyjUrl()),userName);
            }
        }
        if (oldAgreement.getXmBhyjUrl()!=null||newAgreement.getXmBhyjUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmBhyjUrl()).equals(safeToString(newAgreement.getXmBhyjUrl()))){
                insertBAgreementLog(id,"xm_bhyj_url","传保函依据url",
                        safeToString(newAgreement.getXmBhyjUrl()),safeToString(oldAgreement.getXmBhyjUrl()),userName);
            }
        }
        if (oldAgreement.getXmGqyjUrl()!=null||newAgreement.getXmGqyjUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getXmGqyjUrl()).equals(safeToString(newAgreement.getXmGqyjUrl()))){
                insertBAgreementLog(id,"xm_gqyj_url","工期依据url",
                        safeToString(newAgreement.getXmGqyjUrl()),safeToString(oldAgreement.getXmGqyjUrl()),userName);
            }
        }
        if (oldAgreement.getQyName()!=null||newAgreement.getQyName()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyName()).equals(safeToString(newAgreement.getQyName()))){
                insertBAgreementLog(id,"qy_name","企业名称",
                        safeToString(newAgreement.getQyName()),safeToString(oldAgreement.getQyName()),userName);
            }
        }
        if (oldAgreement.getQySheng()!=null||newAgreement.getQySheng()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQySheng()).equals(safeToString(newAgreement.getQySheng()))){
                insertBAgreementLog(id,"qy_sheng","企业省",
                        safeToString(newAgreement.getQySheng()),safeToString(oldAgreement.getQySheng()),userName);
            }
        }

        if (oldAgreement.getQyShi()!=null||newAgreement.getQyShi()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyShi()).equals(safeToString(newAgreement.getQyShi()))){
                insertBAgreementLog(id,"qy_shi","企业市",
                        safeToString(newAgreement.getQyShi()),safeToString(oldAgreement.getQyShi()),userName);
            }
        }
        if (oldAgreement.getQyAddress()!=null||newAgreement.getQyAddress()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyAddress()).equals(safeToString(newAgreement.getQyAddress()))){
                insertBAgreementLog(id,"qy_address","企业地址",
                        safeToString(newAgreement.getQyAddress()),safeToString(oldAgreement.getQyAddress()),userName);
            }
        }
        if (oldAgreement.getQyZcMoney()!=null||newAgreement.getQyZcMoney()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyZcMoney()).equals(safeToString(newAgreement.getQyZcMoney()))){
                insertBAgreementLog(id,"qy_zc_money","注册资金",
                        safeToString(newAgreement.getQyZcMoney()),safeToString(oldAgreement.getQyZcMoney()),userName);
            }
        }
        if (oldAgreement.getQyClDate()!=null||newAgreement.getQyClDate()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyClDate()).equals(safeToString(newAgreement.getQyClDate()))){
                insertBAgreementLog(id,"qy_cl_date","企业成立时间",
                        dateToString(newAgreement.getQyClDate()),dateToString(oldAgreement.getQyClDate()),userName);
            }
        }

        if (oldAgreement.getQyType()!=null||newAgreement.getQyType()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyType()).equals(safeToString(newAgreement.getQyType()))){
                insertBAgreementLog(id,"qy_type","企业类型",
                        safeToString(newAgreement.getQyType()),safeToString(oldAgreement.getQyType()),userName);
            }
        }
        if (oldAgreement.getQyZizhi()!=null||newAgreement.getQyZizhi()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyZizhi()).equals(safeToString(newAgreement.getQyZizhi()))){
                insertBAgreementLog(id,"qy_zizhi","企业最高资质",
                        safeToString(newAgreement.getQyZizhi()),safeToString(oldAgreement.getQyZizhi()),userName);
            }
        }

        if (oldAgreement.getQySpecial()!=null||newAgreement.getQySpecial()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQySpecial()).equals(safeToString(newAgreement.getQySpecial()))){
                insertBAgreementLog(id,"qy_special","特殊情况",
                        safeToString(newAgreement.getQySpecial()),safeToString(oldAgreement.getQySpecial()),userName);
            }
        }
        if (oldAgreement.getQyFpType()!=null||newAgreement.getQyFpType()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyFpType()).equals(safeToString(newAgreement.getQyFpType()))){
                insertBAgreementLog(id,"qy_fp_type","发票类型",
                        safeToString(newAgreement.getQyFpType()),safeToString(oldAgreement.getQyFpType()),userName);
            }
        }
        if (oldAgreement.getQyKpName()!=null||newAgreement.getQyKpName()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyKpName()).equals(safeToString(newAgreement.getQyKpName()))){
                insertBAgreementLog(id,"qy_kp_name","开票名称",
                        safeToString(newAgreement.getQyKpName()),safeToString(oldAgreement.getQyKpName()),userName);
            }
        }
        if (oldAgreement.getQyKpNum()!=null||newAgreement.getQyKpNum()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyKpNum()).equals(safeToString(newAgreement.getQyKpNum()))){
                insertBAgreementLog(id,"qy_kp_num","税号",
                        safeToString(newAgreement.getQyKpNum()),safeToString(oldAgreement.getQyKpNum()),userName);
            }
        }
        if (oldAgreement.getQyKpPhone()!=null||newAgreement.getQyKpPhone()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyKpPhone()).equals(safeToString(newAgreement.getQyKpPhone()))){
                insertBAgreementLog(id,"qy_kp_phone","开票电话",
                        safeToString(newAgreement.getQyKpPhone()),safeToString(oldAgreement.getQyKpPhone()),userName);
            }
        }
        if (oldAgreement.getQyKpAddress()!=null||newAgreement.getQyKpAddress()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyKpAddress()).equals(safeToString(newAgreement.getQyKpAddress()))){
                insertBAgreementLog(id,"qy_kp_address","开票地址",
                        safeToString(newAgreement.getQyKpAddress()),safeToString(oldAgreement.getQyKpAddress()),userName);
            }
        }
        if (oldAgreement.getQyKpBank()!=null||newAgreement.getQyKpBank()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyKpBank()).equals(safeToString(newAgreement.getQyKpBank()))){
                insertBAgreementLog(id,"qy_kp_bank","开票开户行",
                        safeToString(newAgreement.getQyKpBank()),safeToString(oldAgreement.getQyKpBank()),userName);
            }
        }
        if (oldAgreement.getQyKpBankNum()!=null||newAgreement.getQyKpBankNum()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyKpBankNum()).equals(safeToString(newAgreement.getQyKpBankNum()))){
                insertBAgreementLog(id,"qy_kp_bank_num","开户账号",
                        safeToString(newAgreement.getQyKpBankNum()),safeToString(oldAgreement.getQyKpBankNum()),userName);
            }
        }
        if (oldAgreement.getQyKpSendAdress()!=null||newAgreement.getQyKpSendAdress()!=null){//原来不为空
            if (!safeToString(oldAgreement.getQyKpSendAdress()).equals(safeToString(newAgreement.getQyKpSendAdress()))){
                insertBAgreementLog(id,"qy_kp_send_adress","邮寄地址",
                        safeToString(newAgreement.getQyKpSendAdress()),safeToString(oldAgreement.getQyKpSendAdress()),userName);
            }
        }
        if (oldAgreement.getBhMoney()!=null||newAgreement.getBhMoney()!=null){//原来不为空
            if (oldAgreement.getBhMoney()!=null&&newAgreement.getBhMoney()!=null){//新的值不为空
                if (oldAgreement.getBhMoney().doubleValue()!=newAgreement.getBhMoney().doubleValue()){
                    insertBAgreementLog(id,"bh_money","保函金额",
                            newAgreement.getBhMoney().toString(),oldAgreement.getBhMoney().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"bh_money","保函金额",
                        safeToString(newAgreement.getBhMoney()),safeToString(oldAgreement.getBhMoney()),userName);
            }
        }
        if (oldAgreement.getBhGeshi()!=null||newAgreement.getBhGeshi()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBhGeshi()).equals(safeToString(newAgreement.getBhGeshi()))){
                insertBAgreementLog(id,"bh_geshi","保函格式",
                        safeToString(newAgreement.getBhGeshi()),safeToString(oldAgreement.getBhGeshi()),userName);
            }
        }
        if (oldAgreement.getBhGeshiUrl()!=null||newAgreement.getBhGeshiUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBhGeshiUrl()).equals(safeToString(newAgreement.getBhGeshiUrl()))){
                insertBAgreementLog(id,"bh_geshi_url","格式上传url",
                        safeToString(newAgreement.getBhGeshiUrl()),safeToString(oldAgreement.getBhGeshiUrl()),userName);
            }
        }
        if (oldAgreement.getBhRemark()!=null||newAgreement.getBhRemark()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBhRemark()).equals(safeToString(newAgreement.getBhRemark()))){
                insertBAgreementLog(id,"bh_remark","特殊要求",
                        safeToString(newAgreement.getBhRemark()),safeToString(oldAgreement.getBhRemark()),userName);
            }
        }
        if (oldAgreement.getFkMoney()!=null||newAgreement.getFkMoney()!=null){//原来不为空
            if (oldAgreement.getFkMoney()!=null&&newAgreement.getFkMoney()!=null){//新的值不为空
                if (oldAgreement.getFkMoney().doubleValue()!=newAgreement.getFkMoney().doubleValue()){
                    insertBAgreementLog(id,"fk_money","收费",
                            newAgreement.getFkMoney().toString(),oldAgreement.getFkMoney().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"fk_money","收费",
                        safeToString(newAgreement.getFkMoney()),safeToString(oldAgreement.getFkMoney()),userName);
            }
        }
        if (oldAgreement.getFkType()!=null||newAgreement.getFkType()!=null){//原来不为空
            if (!safeToString(oldAgreement.getFkType()).equals(safeToString(newAgreement.getFkType()))){
                insertBAgreementLog(id,"fk_type","付款方式",
                        safeToString(newAgreement.getFkType()),safeToString(oldAgreement.getFkType()),userName);
            }
        }
        if (oldAgreement.getFkUrl()!=null||newAgreement.getFkUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getFkUrl()).equals(safeToString(newAgreement.getFkUrl()))){
                insertBAgreementLog(id,"fk_url","付款凭证",
                        safeToString(newAgreement.getFkUrl()),safeToString(oldAgreement.getFkUrl()),userName);
            }
        }
        if (oldAgreement.getFkHzMoney()!=null||newAgreement.getFkHzMoney()!=null){//原来不为空
            if (!safeToString(oldAgreement.getFkHzMoney()).equals(safeToString(newAgreement.getFkHzMoney()))){
                insertBAgreementLog(id,"fk_hz_money","合作方费用",
                        safeToString(newAgreement.getFkHzMoney()),safeToString(oldAgreement.getFkHzMoney()),userName);
            }
        }
        if (oldAgreement.getFkHzUrl()!=null||newAgreement.getFkHzUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getFkHzUrl()).equals(safeToString(newAgreement.getFkHzUrl()))){
                insertBAgreementLog(id,"fk_hz_url","合作方付款",
                        safeToString(newAgreement.getFkHzUrl()),safeToString(oldAgreement.getFkHzUrl()),userName);
            }
        }
        if (oldAgreement.getFkHzType()!=null||newAgreement.getFkHzType()!=null){//原来不为空
            if (!safeToString(oldAgreement.getFkHzType()).equals(safeToString(newAgreement.getFkHzType()))){
                insertBAgreementLog(id,"fk_hz_type","合作方付款方式",
                        safeToString(newAgreement.getFkHzType()),safeToString(oldAgreement.getFkHzType()),userName);
            }
        }
        if (oldAgreement.getFkShouyi()!=null||newAgreement.getFkShouyi()!=null){//原来不为空
            if (oldAgreement.getFkShouyi()!=null&&newAgreement.getFkShouyi()!=null){//新的值不为空
                if (oldAgreement.getFkShouyi().doubleValue()!=newAgreement.getFkShouyi().doubleValue()){
                    insertBAgreementLog(id,"fk_shouyi","公司收益",
                            newAgreement.getFkShouyi().toString(),oldAgreement.getFkShouyi().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"fk_shouyi","公司收益",
                        safeToString(newAgreement.getFkShouyi()),safeToString(oldAgreement.getFkShouyi()),userName);
            }
        }
        if (oldAgreement.getFkFanrun()!=null||newAgreement.getFkFanrun()!=null){//原来不为空
            if (oldAgreement.getFkFanrun()!=null&&newAgreement.getFkFanrun()!=null){//新的值不为空
                if (oldAgreement.getFkFanrun().doubleValue()!=newAgreement.getFkFanrun().doubleValue()){
                    insertBAgreementLog(id,"fk_fanrun","返润扣税",
                            newAgreement.getFkFanrun().toString(),oldAgreement.getFkFanrun().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"fk_fanrun","返润扣税",
                        safeToString(newAgreement.getFkFanrun()),safeToString(oldAgreement.getFkFanrun()),userName);
            }
        }
        if (oldAgreement.getYyAcceptanceOpinion()!=null||newAgreement.getYyAcceptanceOpinion()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYyAcceptanceOpinion()).equals(safeToString(newAgreement.getYyAcceptanceOpinion()))){
                insertBAgreementLog(id,"yy_acceptance_opinion","受理意见",
                        safeToString(newAgreement.getYyAcceptanceOpinion()),safeToString(oldAgreement.getYyAcceptanceOpinion()),userName);
            }
        }
        if (oldAgreement.getYyPrice()!=null||newAgreement.getYyPrice()!=null){//原来不为空
            if (oldAgreement.getYyPrice()!=null&&newAgreement.getYyPrice()!=null){//新的值不为空
                if (oldAgreement.getYyPrice().doubleValue()!=newAgreement.getYyPrice().doubleValue()){
                    insertBAgreementLog(id,"yy_price","价格",
                            newAgreement.getYyPrice().toString(),oldAgreement.getYyPrice().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"yy_price","价格",
                        safeToString(newAgreement.getYyPrice()),safeToString(oldAgreement.getYyPrice()),userName);
            }
        }
        if (oldAgreement.getYySupplyUrl()!=null||newAgreement.getYySupplyUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYySupplyUrl()).equals(safeToString(newAgreement.getYySupplyUrl()))){
                insertBAgreementLog(id,"yy_supply_url","补充资料url",
                        safeToString(newAgreement.getYySupplyUrl()),safeToString(oldAgreement.getYySupplyUrl()),userName);
            }
        }
        if (oldAgreement.getYySpecialRemark()!=null||newAgreement.getYySpecialRemark()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYySpecialRemark()).equals(safeToString(newAgreement.getYySpecialRemark()))){
                insertBAgreementLog(id,"yy_specials_remark","特殊备注",
                        safeToString(newAgreement.getYySpecialRemark()),safeToString(oldAgreement.getYySpecialRemark()),userName);
            }
        }
        if (oldAgreement.getYyContractData()!=null||newAgreement.getYyContractData()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYyContractData()).equals(safeToString(newAgreement.getYyContractData()))){
                insertBAgreementLog(id,"yy_contract_data","签约资料包url",
                        safeToString(newAgreement.getYyContractData()),safeToString(oldAgreement.getYyContractData()),userName);
            }
        }
        if (oldAgreement.getYyContractHg()!=null||newAgreement.getYyContractHg()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYyContractHg()).equals(safeToString(newAgreement.getYyContractHg()))){
                insertBAgreementLog(id,"yy_contract_hg","资料包是否合格",
                        safeToString(newAgreement.getYyContractHg()),safeToString(oldAgreement.getYyContractHg()),userName);
            }
        }
        if (oldAgreement.getYySupplyRemark()!=null||newAgreement.getYySupplyRemark()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYySupplyRemark()).equals(safeToString(newAgreement.getYySupplyRemark()))){
                insertBAgreementLog(id,"yy_supply_remark","补充文件描述",
                        safeToString(newAgreement.getYySupplyRemark()),safeToString(oldAgreement.getYySupplyRemark()),userName);
            }
        }
        if (oldAgreement.getYyFandanbaoqianshu()!=null||newAgreement.getYyFandanbaoqianshu()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYyFandanbaoqianshu()).equals(safeToString(newAgreement.getYyFandanbaoqianshu()))){
                insertBAgreementLog(id,"yy_fandanbaoqianshu","反担保签署",
                        safeToString(newAgreement.getYyFandanbaoqianshu()),safeToString(oldAgreement.getYyFandanbaoqianshu()),userName);
            }
        }
        if (oldAgreement.getYyYoujidanhao()!=null||newAgreement.getYyYoujidanhao()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYyYoujidanhao()).equals(safeToString(newAgreement.getYyYoujidanhao()))){
                insertBAgreementLog(id,"yy_youjidanhao","邮寄单号",
                        safeToString(newAgreement.getYyYoujidanhao()),safeToString(oldAgreement.getYyYoujidanhao()),userName);
            }
        }
        if (oldAgreement.getYyBaohanUrl()!=null||newAgreement.getYyBaohanUrl()!=null){//原来不为空
            if (!safeToString(oldAgreement.getYyBaohanUrl()).equals(safeToString(newAgreement.getYyBaohanUrl()))){
                insertBAgreementLog(id,"yy_baohan_url","上传保函",
                        safeToString(newAgreement.getYyBaohanUrl()),safeToString(oldAgreement.getYyBaohanUrl()),userName);
            }
        }
        if (oldAgreement.getYwTotalMoney()!=null||newAgreement.getYwTotalMoney()!=null){//原来不为空
            if (oldAgreement.getYwTotalMoney()!=null&&newAgreement.getYwTotalMoney()!=null){//新的值不为空
                if (oldAgreement.getYwTotalMoney().doubleValue()!=newAgreement.getYwTotalMoney().doubleValue()){
                    insertBAgreementLog(id,"yw_total_money","总费用",
                            newAgreement.getYwTotalMoney().toString(),oldAgreement.getYwTotalMoney().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"yw_total_money","总费用",
                        safeToString(newAgreement.getYwTotalMoney()),safeToString(oldAgreement.getYwTotalMoney()),userName);
            }
        }
        if (oldAgreement.getBankMoney()!=null||newAgreement.getBankMoney()!=null){//原来不为空
            if (oldAgreement.getBankMoney()!=null&&newAgreement.getBankMoney()!=null){//新的值不为空
                if (oldAgreement.getBankMoney().doubleValue()!=newAgreement.getBankMoney().doubleValue()){
                    insertBAgreementLog(id,"bank_money","银行成本",
                            newAgreement.getBankMoney().toString(),oldAgreement.getBankMoney().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"bank_money","银行成本",
                        safeToString(newAgreement.getBankMoney()),safeToString(oldAgreement.getBankMoney()),userName);
            }
        }
        if (oldAgreement.getBankAccount()!=null||newAgreement.getBankAccount()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBankAccount()).equals(safeToString(newAgreement.getBankAccount()))){
                insertBAgreementLog(id,"bank_account","银行收款账户",
                        safeToString(newAgreement.getBankAccount()),safeToString(oldAgreement.getBankAccount()),userName);
            }
        }

        if (oldAgreement.getBankSf()!=null||newAgreement.getBankSf()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBankSf()).equals(safeToString(newAgreement.getBankSf()))){
                insertBAgreementLog(id,"bank_sf","是否收到",
                        safeToString(newAgreement.getBankSf()),safeToString(oldAgreement.getBankSf()),userName);
            }
        }
        if (oldAgreement.getBankMoney2()!=null||newAgreement.getBankMoney2()!=null){//原来不为空
            if (oldAgreement.getBankMoney2()!=null&&newAgreement.getBankMoney2()!=null){//新的值不为空
                if (oldAgreement.getBankMoney2().doubleValue()!=newAgreement.getBankMoney2().doubleValue()){
                    insertBAgreementLog(id,"bank_money2","银行成本2",
                            newAgreement.getBankMoney2().toString(),oldAgreement.getBankMoney2().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"bank_money2","银行成本2",
                        safeToString(newAgreement.getBankMoney2()),safeToString(oldAgreement.getBankMoney2()),userName);
            }
        }
        if (oldAgreement.getBankAccount2()!=null||newAgreement.getBankAccount2()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBankAccount2()).equals(safeToString(newAgreement.getBankAccount2()))){
                insertBAgreementLog(id,"bank_account2","银行账户2",
                        safeToString(newAgreement.getBankAccount2()),safeToString(oldAgreement.getBankAccount2()),userName);
            }
        }
        if (oldAgreement.getBankSf2()!=null||newAgreement.getBankSf2()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBankSf2()).equals(safeToString(newAgreement.getBankSf2()))){
                insertBAgreementLog(id,"bank_sf2","是否收到2",
                        safeToString(newAgreement.getBankSf2()),safeToString(oldAgreement.getBankSf2()),userName);
            }
        }
        if (oldAgreement.getYwFanMoney()!=null||newAgreement.getYwFanMoney()!=null){//原来不为空
            if (oldAgreement.getYwFanMoney()!=null&&newAgreement.getYwFanMoney()!=null){//新的值不为空
                if (oldAgreement.getYwFanMoney().doubleValue()!=newAgreement.getYwFanMoney().doubleValue()){
                    insertBAgreementLog(id,"yw_fan_money","返润",
                            newAgreement.getYwFanMoney().toString(),oldAgreement.getYwFanMoney().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"yw_fan_money","返润",
                        safeToString(newAgreement.getYwFanMoney()),safeToString(oldAgreement.getYwFanMoney()),userName);
            }
        }
        if (oldAgreement.getYwTaxMoney()!=null||newAgreement.getYwTaxMoney()!=null){//原来不为空
            if (oldAgreement.getYwTaxMoney()!=null&&newAgreement.getYwTaxMoney()!=null){//新的值不为空
                if (oldAgreement.getYwTaxMoney().doubleValue()!=newAgreement.getYwTaxMoney().doubleValue()){
                    insertBAgreementLog(id,"yw_tax_money","扣税",
                            newAgreement.getYwTaxMoney().toString(),oldAgreement.getYwTaxMoney().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"yw_tax_money","扣税",
                        safeToString(newAgreement.getYwTaxMoney()),safeToString(oldAgreement.getYwTaxMoney()),userName);
            }
        }
        if (oldAgreement.getYwYeji()!=null||newAgreement.getYwYeji()!=null){//原来不为空
            if (oldAgreement.getYwYeji()!=null&&newAgreement.getYwYeji()!=null){//新的值不为空
                if (oldAgreement.getYwYeji().doubleValue()!=newAgreement.getYwYeji().doubleValue()){
                    insertBAgreementLog(id,"yw_yeji","业绩",
                            newAgreement.getYwYeji().toString(),oldAgreement.getYwYeji().toString(),userName);
                }
            }else {
                insertBAgreementLog(id,"yw_yeji","业绩",
                        safeToString(newAgreement.getYwYeji()),safeToString(oldAgreement.getYwYeji()),userName);
            }
        }
        if (oldAgreement.getSfKp()!=null||newAgreement.getSfKp()!=null){//原来不为空
            if (!safeToString(oldAgreement.getSfKp()).equals(safeToString(newAgreement.getSfKp()))){
                insertBAgreementLog(id,"sfKp","是否开票",
                        safeToString(newAgreement.getSfKp()),safeToString(oldAgreement.getSfKp()),userName);
            }
        }
        if (oldAgreement.getBhDate()!=null||newAgreement.getBhDate()!=null){//原来不为空
            if (!safeToString(oldAgreement.getBhDate()).equals(safeToString(newAgreement.getBhDate()))){
                insertBAgreementLog(id,"bh_date","出函日期",
                        dateToString(newAgreement.getBhDate()),dateToString(oldAgreement.getBhDate()),userName);
            }
        }
        if (oldAgreement.getMailNum()!=null||newAgreement.getMailNum()!=null){//原来不为空
            if (!safeToString(oldAgreement.getMailNum()).equals(safeToString(newAgreement.getMailNum()))){
                insertBAgreementLog(id,"mail_num","邮寄单号",
                        safeToString(newAgreement.getMailNum()),safeToString(oldAgreement.getMailNum()),userName);
            }
        }
        newAgreement.setUpdateTime(DateUtils.getNowDate());
        return bAgreementMapper.updateBAgreement(newAgreement);
    }

    public String dateToString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return (date != null) ? formatter.format(date) : "";
    }
    public String safeToString(Object obj) {
        return (obj != null) ? obj.toString() : "";
    }

    private void insertBAgreementLog(Long id,String field,String name,String value,String oldValue, String userName){
        BAgreementLog agreementLog = new BAgreementLog();
        agreementLog.setAgreementId(id);
        agreementLog.setbField(field);
        agreementLog.setbName(name);
        agreementLog.setbValue(value);
        agreementLog.setbValueOld(oldValue);
        agreementLog.setCreateTime(DateUtils.getNowDate());
        agreementLog.setCreateBy(userName);
        bAgreementLogMapper.insertBAgreementLog(agreementLog);
    }
}
